package com.behome.netty.common;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegularKit {
    public static final String RE_CHINESE = "[一-\u9fff]";
    public static final Pattern GROUP_VAR = Pattern.compile("\\$(\\d+)");
    public static final String regExp_integer_1 = "^\\d+$";
    public static final String regExp_integer_2 = "^[0-9]*[1-9][0-9]*$";
    public static final String regExp_integer_3 = "^((-\\d+) ?(0+))$";
    public static final String regExp_integer_4 = "^-[0-9]*[1-9][0-9]*$";
    public static final String regExp_integer_5 = "^-?\\d+$";
    public static final String regExp_float_1 = "^\\d+(\\.\\d+)?$";
    public static final String regExp_float_2 = "^(([0-9]+\\.[0-9]*[1-9][0-9]*) ?([0-9]*[1-9][0-9]*\\.[0-9]+) ?([0-9]*[1-9][0-9]*))$";
    public static final String regExp_float_3 = "^((-\\d+(\\.\\d+)?) ?(0+(\\.0+)?))$";
    public static final String regExp_float_4 = "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*) ?([0-9]*[1-9][0-9]*\\.[0-9]+) ?([0-9]*[1-9][0-9]*)))$";
    public static final String regExp_float_5 = "^(-?\\d+)(\\.\\d+)?$";
    public static final String regExp_letter_1 = "^[A-Za-z]+$";
    public static final String regExp_letter_2 = "^[A-Z]+$";
    public static final String regExp_letter_3 = "^[a-z]+$";
    public static final String regExp_letter_4 = "^[A-Za-z0-9]+$";
    public static final String regExp_letter_5 = "^\\w+$";
    public static final String regExp_email = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
    public static final String regExp_url_1 = "^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";
    public static final String regExp_url_2 = "[a-zA-z]+://[^\\s]*";
    public static final String regExp_chinese_1 = "[\\u4e00-\\u9fa5]";
    public static final String regExp_chinese_2 = "[^\\x00-\\xff]";
    public static final String regExp_line = "\\n[\\s ? ]*\\r";
    public static final String regExp_html_1 = "/ <(.*)>.* <\\/\\1> ? <(.*) \\/>/";
    public static final String regExp_startEndEmpty = "(^\\s*) ?(\\s*$)";
    public static final String regExp_accountNumber = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
    public static final String regExp_telephone = "\\d{11}";
    public static final String regExp_qq = "[1-9][0-9]{4,}";
    public static final String regExp_postbody = "[1-9]\\d{5}(?!\\d)";
    public static final String regExp_idCard = "\\d{15} ?\\d{18}";
    public static final String regExp_ip = "^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
    public static final String regExp_ip_port = "^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):(\\d{1,5})$";

    private RegularKit() {
    }

    public static String get(String regex, String content, int groupIndex) {
        Pattern pattern = Pattern.compile(regex, 32);
        return get(pattern, content, groupIndex);
    }

    public static String get(Pattern pattern, String content, int groupIndex) {
        if(null == content) {
            return null;
        } else {
            Matcher matcher = pattern.matcher(content);
            return matcher.find()?matcher.group(groupIndex):null;
        }
    }

    public static String extractMulti(Pattern pattern, String content, String template) {
        HashSet<String> varNums = (HashSet)findAll((Pattern)GROUP_VAR, template, 1, new HashSet());
        Matcher matcher = pattern.matcher(content);
        if(!matcher.find()) {
            return null;
        } else {
            String var;
            int group;
            for(Iterator var5 = varNums.iterator(); var5.hasNext(); template = template.replace("$" + var, matcher.group(group))) {
                var = (String)var5.next();
                group = Integer.parseInt(var);
            }

            return template;
        }
    }

    public static String extractMulti(String regex, String content, String template) {
        Pattern pattern = Pattern.compile(regex, 32);
        return extractMulti(pattern, content, template);
    }

    public static String delFirst(String regex, String content) {
        return content.replaceFirst(regex, "");
    }

    public static <T extends Collection<String>> T findAll(String regex, String content, int group, T collection) {
        Pattern pattern = Pattern.compile(regex, 32);
        return findAll(pattern, content, group, collection);
    }

    public static <T extends Collection<String>> T findAll(Pattern pattern, String content, int group, T collection) {
        Matcher matcher = pattern.matcher(content);

        while(matcher.find()) {
            collection.add(matcher.group(group));
        }

        return collection;
    }

    public static boolean isMatch(Pattern pattern, String content) {
        return content != null && pattern != null?pattern.matcher(content).matches():false;
    }

    public static boolean isExist(String regex, String str) {
        Pattern pat = Pattern.compile(regex);
        Matcher mat = pat.matcher(str);
        return mat.find();
    }

    public static String replaceAll(String regex, String ment, String str, int flags) {
        Pattern pat = null;
        if(flags == -1) {
            pat = Pattern.compile(regex);
        } else {
            pat = Pattern.compile(regex, flags);
        }

        Matcher mat = pat.matcher(str);
        return mat.replaceAll(ment);
    }

    public static String[] getMatcherValue(String regex, String str) {
        return getMatcherValue(regex, str, 2);
    }

    public static String[] getMatcherValue(String regex, String str, int flags) {
        Pattern pat = null;
        if(flags == -1) {
            pat = Pattern.compile(regex);
        } else {
            pat = Pattern.compile(regex, flags);
        }

        Matcher mat = pat.matcher(str);
        if(!mat.find()) {
            return null;
        } else {
            String[] param = new String[mat.groupCount()];

            for(int i = 0; i < mat.groupCount(); ++i) {
                param[i] = mat.group(i + 1);
            }

            return param;
        }
    }

    public static boolean isTrue(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher match = pattern.matcher(str);
        return match.matches();
    }

    public static Map<String, String> jsonStrToKVMap(String jsonStr) {
        Map<String, String> ret = new HashMap();
        Pattern pattern = Pattern.compile("(?<=\\(|\\{)[^\\)|\\}]+");
        Matcher matcher = pattern.matcher(jsonStr);
        ArrayList tmp = new ArrayList();

        while(matcher.find()) {
            tmp.add(matcher.group());
        }

        if(tmp.size() > 0) {
            Iterator var5 = tmp.iterator();

            while(var5.hasNext()) {
                String item = (String)var5.next();
                String[] kvs = item.split(",");
                String[] var8 = kvs;
                int var9 = kvs.length;

                for(int var10 = 0; var10 < var9; ++var10) {
                    String kv = var8[var10];
                    if(kv.length() > 0) {
                        String[] pairs = kv.split(":");
                        if(pairs.length == 2) {
                            if(ret.containsKey(pairs[0])) {
                                ret.put(pairs[0] + "_1", pairs[1]);
                            } else {
                                ret.put(pairs[0], pairs[1]);
                            }
                        }
                    }
                }
            }
        }

        return ret;
    }

    public static void main(String[] args) {
        String string = "{\"故障标识\":\"故障消除\",\"故障类型\":\"datadic(terminalFaultType,1)\",\"故障描述\":\"test\"}";
        JSONObject object = JSON.parseObject(string);
        System.out.println(object.getString("故障类型"));
    }
}
